package com.stone.jvm;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * JDK1.8
 * -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M
 */
public class OOMTest extends ClassLoader {

    public static void main(String[] args) {
        int j = 0;
        try {
            OOMTest test = new OOMTest();
            for (int i = 0; i < 10000; i++) {
                // 创建 ClassWriter 对象，用于生产类的二进制字节码
                ClassWriter classWriter = new ClassWriter(0);
                // 指明版本号，publc , 类名， 包名，父类，接口
                classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class" + i, null, "java/lang/Object", null);
                byte[] code = classWriter.toByteArray();
                // 类的加载
                test.defineClass("Class" + i, code, 0, code.length); // Class 对象
                j++;
            }
        } finally {
            System.err.println(j);

        }
    }

}
